package me.zhengjie.domain;

import me.zhengjie.dto.sqlServer.Pallet;
import org.apache.poi.ss.usermodel.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.dto.sqlServer.Storage;
import me.zhengjie.repository.StorageRepository;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Iterator;

@Component
@RequiredArgsConstructor
public class FileService {

    private final StorageRepository storageRepository;

    /**
     * 上传储位excel表，将信息加载到数据库中
     * @param file 前端发送的excel文件
     * @throws IOException io异常
     */
    public void saveStorageExcelData(MultipartFile file) throws IOException {
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);

        Iterator<Row> iterator = sheet.iterator();
        iterator.next(); // 跳过表头

        while (iterator.hasNext()) {
            Row row = iterator.next();
            Storage storage = new Storage();
            storage.setLogicalAddressType(1);
            Cell cell1 = row.getCell(1);
            cell1.setCellType(CellType.STRING);
            storage.setStorageCode(cell1.getStringCellValue());

            Cell cell2 = row.getCell(2);
            if (cell2==null){
                storage.setPalletId(null);
            }else {
                cell2.setCellType(CellType.STRING);
                storage.setPalletId(Integer.valueOf(cell2.getStringCellValue()));
            }

            Cell cell3 = row.getCell(3);
            if (cell3==null){
                storage.setWorkNum(null);
            }else {
                cell3.setCellType(CellType.STRING);
                storage.setWorkNum(cell3.getStringCellValue());
            }

            Cell cell4 = row.getCell(4);
            cell4.setCellType(CellType.STRING);
            storage.setZoneCode(cell4.getStringCellValue());

            Cell cell5 = row.getCell(5);
            cell5.setCellType(CellType.STRING);
            storage.setAisle(Integer.valueOf(cell5.getStringCellValue()));

            Cell cell6 = row.getCell(6);
            cell6.setCellType(CellType.STRING);
            storage.setLayer(Integer.valueOf(row.getCell(6).getStringCellValue()));

            Cell cell7 = row.getCell(7);
            cell7.setCellType(CellType.STRING);
            storage.setRow(Integer.valueOf(row.getCell(7).getStringCellValue()));

            Cell cell8 = row.getCell(8);
            cell8.setCellType(CellType.STRING);
            storage.setColumn(Integer.valueOf(row.getCell(8).getStringCellValue()));

            Cell cell9 = row.getCell(9);
            cell9.setCellType(CellType.STRING);
            storage.setLocationInventoryStatus(Integer.valueOf(row.getCell(9).getStringCellValue()));

            Cell cell10 = row.getCell(10);
            cell10.setCellType(CellType.STRING);
            storage.setStatus(Integer.valueOf(row.getCell(10).getStringCellValue()));

            storageRepository.save(storage);
        }
        workbook.close();
    }
}
